/* 
* sapdp.h
*
* Copyright (c) 2010, Zachary N J Peterson <znpeters@nps.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Naval Postgraduate School nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ZACHARY N J PETERSON ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZACHARY N J PETERSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __SAPDP_H__
#define __SAPDP_H__

#include <openssl/bn.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>

#define SAPDP_BLOCK_SIZE 4096
#define USE_FDPRF

#define PRF_KEY_SIZE 20
#define RSA_KEY_SIZE 1024
#define RSA_E RSA_F4

typedef BIGNUM SAPDP_generator;
typedef BIGNUM DPDP_generator;

typedef struct SAPDP_key_struct DPDP_key;
typedef struct SAPDP_key_struct SAPDP_key;

struct SAPDP_key_struct{

	RSA *rsa;				/* RSA key pair */
	unsigned char *k_prf;	/* a symmetric PRF key */
	SAPDP_generator *g;		/* generator of the group Z*N */	
};

typedef struct SAPDP_client_tag_struct SAPDP_client_tag;

struct SAPDP_client_tag_struct{
	BIGNUM *b;
	BIGNUM *e;
	BIGNUM *z;
#ifdef USE_FDPRF
	BIGNUM *r;
#else
	BIGNUM *r1;
	BIGNUM *r2;
#endif
};

typedef struct SAPDP_server_tag_struct SAPDP_server_tag;

struct SAPDP_server_tag_struct{
	BIGNUM *x;
	BIGNUM *y;
};

typedef struct DPDP_tag_struct DPDP_tag;

struct DPDP_tag_struct{
	BIGNUM *M;
};


SAPDP_client_tag *sapdp_client_tag_block(SAPDP_key *key, unsigned char *block, size_t blocksize, unsigned int index);
SAPDP_server_tag *sapdp_server_tag_block(SAPDP_key *key, SAPDP_client_tag *ctag);
DPDP_tag *dpdp_tag_block(DPDP_key *key, unsigned char *block, size_t blocksize);

void sapdp_destroy_key(SAPDP_key *key);
SAPDP_key *sapdp_generate_key();
DPDP_key *dpdp_generate_key();

void printhex(unsigned char *ptr, size_t size);
void sfree(void *ptr, size_t size);
BIGNUM *generate_fdprf_f(SAPDP_key *key, unsigned char *input_prf, size_t input_prf_size);
void destroy_sapdp_client_tag(SAPDP_client_tag *tag);
SAPDP_client_tag *generate_sapdp_client_tag();
void destroy_sapdp_server_tag(SAPDP_server_tag *tag);
SAPDP_server_tag *generate_sapdp_server_tag();
void destroy_dpdp_tag(DPDP_tag *tag);
DPDP_tag *generate_dpdp_tag();

#endif